package com.znsb.msfq.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.znsb.msfq.BaseActivity;
import com.znsb.msfq.R;
import com.znsb.msfq.app.ZnsbApp;
import com.znsb.msfq.bean.UserInfoBean;
import com.znsb.msfq.dialog.PersionDialog;
import com.znsb.msfq.utils.ActivityUtil;
import com.znsb.msfq.utils.ConfigUtils;
import com.znsb.msfq.utils.GsonUtils;
import com.znsb.msfq.utils.HttpUtils;
import com.znsb.msfq.utils.LogUtils;
import com.znsb.msfq.utils.ResponseUtils;
import com.znsb.msfq.utils.SPUtils;
import com.znsb.msfq.utils.StringUtils;
import com.znsb.msfq.utils.ToastUtils;
import com.znsb.msfq.utils.UrlUtils;
import com.znsb.msfq.utils.ValidateUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @name： 我的资料
 * @author： 杨 广
 * @phone： 17382373271
 * @createTime： 2017/2/10
 * @modifyTime： 2017/2/10
 * @explain：
 */

public class PersonalDetailsActivity extends BaseActivity {


    @Bind(R.id.title_back)
    TextView titleBack;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.title_next)
    TextView titleNext;
    @Bind(R.id.title_layout)
    LinearLayout titleLayout;
    @Bind(R.id.pda_tv_sex)
    TextView pdaTvSex;
    @Bind(R.id.pda_tv_age)
    TextView pdaTvAge;
    @Bind(R.id.pda_tv_occupation)
    TextView pdaTvOccupation;
    @Bind(R.id.pda_tv_username)
    TextView pdaTvUsername;
    private int sexs = 0;

    private ArrayList<String> listsex;
    private ArrayList<String> listage;
    private ArrayList<String> listoccupation;

    @Override
    protected int getlayoutView() {
        return R.layout.act_personaldetails;
    }

    @Override
    protected void initView(View view) {

    }

    @Override
    protected void initData() {
        titleText.setText("我的资料");
        titleNext.setText("保存");
        titleNext.setTextColor(Color.rgb(251, 78, 64));
        listsex = new ArrayList<>();
        listage = new ArrayList<>();
        listoccupation = new ArrayList<>();
        listsex.add("男");
        listsex.add("女");
        for (int i = 16; i < 100; i++) {
            listage.add("" + i);
        }
        listoccupation.add("学生族");
        listoccupation.add("工薪族");
        listoccupation.add("自由族");
        listoccupation.add("其他");
        refreshView();
    }

    /**
     * 更新个人资料数据
     */
    private UserInfoBean userInfo = null;

    private void refreshView() {
        userInfo = SPUtils.getUserInfo();
        pdaTvUsername.setText(ValidateUtils.getShowHint(userInfo.getPhone(), 3, 8));
        pdaTvSex.setText(userInfo.getSex() + "");
        pdaTvAge.setText(userInfo.getAge() + "");
        pdaTvOccupation.setText(userInfo.getProfession() + "");
    }

    @Override
    protected void clickListener(View view) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.title_back, R.id.title_next, R.id.pda_tv_sex, R.id.pda_tv_age, R.id.pda_tv_occupation})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                ActivityUtil.goBack(PersonalDetailsActivity.this);
                break;
            case R.id.title_next:
                String age = pdaTvAge.getText().toString();
                String sex = pdaTvSex.getText().toString();
                String profession = pdaTvOccupation.getText().toString();
                if (StringUtils.isNotNull(age) && StringUtils.isNotNull(sex) && StringUtils.isNotNull(profession)) {
                    if (sex.equals("男")) {
                        sexs = 1;
                    }
                    setUserInfo(Integer.parseInt(age), sexs, profession);
                } else {
                    ToastUtils.TextToast(ZnsbApp.mContext, getResources().getString(R.string.noinformation), 2000);
                }
                break;
            case R.id.pda_tv_sex:
                PersionDialog.getinit(PersonalDetailsActivity.this, listsex, pdaTvSex, pdaTvSex.getText().toString().trim());
                break;
            case R.id.pda_tv_age:
                PersionDialog.getinit(PersonalDetailsActivity.this, listage, pdaTvAge, pdaTvAge.getText().toString().trim());
                break;
            case R.id.pda_tv_occupation:
                PersionDialog.getinit(PersonalDetailsActivity.this, listoccupation, pdaTvOccupation, pdaTvOccupation.getText().toString().trim());
                break;
        }
    }

    /**
     * 获取个人资料
     */
    private void getUserInfo() {
        HttpUtils.getInstance(this).postAsnyRequest(UrlUtils.MSFQ_URL_INFO, HttpUtils.getLoginMaps(null), new ResponseUtils() {
            @Override
            public void onSucceed(String Code, JSONObject request) {
                LogUtils.i("TAG:", "userinfo=" + request.toString());
                UserInfoBean data = (UserInfoBean) GsonUtils.getGsonData(UserInfoBean.class, request.toString());
                if (data != null) {
                    SPUtils.putUserInfo(data);
                }
            }

            @Override
            public void onFailure(String result) {

            }
        });
    }

    /**
     * 修改个人资料
     *
     * @param age
     * @param sex
     * @param profession
     */
    private void setUserInfo(final int age, final int sex, final String profession) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("age", age);
        maps.put("sex", sex);
        maps.put("profession", profession);
        HttpUtils.getInstance(this).postAsnyRequest(UrlUtils.MSFQ_URL_UPDATEINFO, HttpUtils.getLoginMaps(maps), new ResponseUtils() {
            @Override
            public void onSucceed(String Code, JSONObject request) {
                LogUtils.i("TAG:", "setuserinfo=" + request.toString());
                userInfo.setAge(age + "");
                userInfo.setSex(sex + "");
                userInfo.setProfession(profession);
                SPUtils.putUserInfo(userInfo);
                finish();
            }

            @Override
            public void onFailure(String result) {

            }
        });
    }

    /**
     * 用户身份认证
     */
    private void setUserIndety() {
        Map<String, Object> maps = new HashMap<>();
        maps.put("realName", "");
        maps.put("identity", "");
        HttpUtils.getInstance(this).postAsnyRequest(UrlUtils.MSFQ_URL_IDENTY, HttpUtils.getLoginMaps(maps), new ResponseUtils() {
            @Override
            public void onSucceed(String Code, JSONObject request) {
                LogUtils.i("TAG:", "setUserIndety=" + request.toString());
            }

            @Override
            public void onFailure(String result) {

            }
        });
    }

    /**
     * 获取用户认证状态
     */
    private void getUserIndety() {
        HttpUtils.getInstance(this).postAsnyRequest(UrlUtils.MSFQ_URL_IDENTYSTATE, HttpUtils.getLoginMaps(null), new ResponseUtils() {
            @Override
            public void onSucceed(String Code, JSONObject request) {
                LogUtils.i("TAG:", "getUserIndety=" + request.toString());
            }

            @Override
            public void onFailure(String result) {

            }
        });
    }

}
